#include "../include/Configuration.h"
#include "../include/logger.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>

using namespace log4cpp;
using std::make_pair;
using std::endl;
using std::cerr;
using std::ifstream;
using std::istringstream;
using std::cout;
Configuration * Configuration::_pInstance = nullptr;
Configuration *Configuration::getInstance()
{
    if(_pInstance == nullptr)
        _pInstance = new Configuration();
    return _pInstance;
}

void Configuration::destory()
{
    if(_pInstance != nullptr)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }
}
bool Configuration::loadConfigData(string filename)
{
    if(!filename.empty())
    {
        //原子操作
        _mux.lock();
        ifstream file(filename);
        if(!file.is_open())
        {
            LOG_ERROR("opern config file");
            return false;
        }

        string line;
        int lineNum = 0;
        while(getline(file, line))
        {
            ++ lineNum;
            processLine(line);
        }
        cout << "===========Configuration.cpp loadConfigData() 46==================" << endl;
        for(auto ele : _configData)
        {
            cout << ele.first << ":" << ele.second << endl;
        }
        cout << "===================================================================" << endl;
        file.close();
        _mux.unlock();
        return true;

    }
    else
    {
        LOG_INFO("config is empty");
        return false;
    }

}

void Configuration::processLine(string &line)
{

    //将每一行
    string key;
    string value;
    istringstream iss(line);
    iss >> key;
    getline(iss >> std::ws, value);
    _configData.insert(make_pair(key,value));
    cout <<"Configuration.cpp processLine()  67," << key <<":" << value << endl;
}

string Configuration::getIp()
{
    auto it = _configData.find("ip");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant get ip");
    return "";
}

unsigned short Configuration::getPort()
{
    auto it = _configData.find("port");
    if(it != _configData.end())
    {
        return (unsigned short)atoi(it->second.c_str());
    }
    LOG_ERROR("cant find port");
    return -1;
}



string Configuration::getLogfile()
{
    auto it = _configData.find("log_file");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant get logfile");
    return "";
}

int Configuration::getThreadNum()
{
    auto it = _configData.find("thread_num");
    if(it != _configData.end())
    {
        return atoi(it->second.c_str());
    }
    LOG_ERROR("cant find threadNum");
    return -1;
}
int Configuration::getTaskNum()
{
    auto it = _configData.find("task_num");
    if(it != _configData.end())
    {
        return atoi(it->second.c_str());
    }
    LOG_ERROR("cant find taskNum");
    return -1;
}
string Configuration::getVedioPath()
{
    auto it = _configData.find("video_path");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant find vediopath");
    return "";
}

string Configuration::getDatabase()
{
    auto it = _configData.find("database");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant get database");
    return "";
}
string Configuration::getLocalhost()
{
    auto it = _configData.find("localhost");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant get localhost");
    return "";
}
string Configuration::getUsername()
{
    auto it = _configData.find("username");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant get username");
    return "";
}
string Configuration::getPasssword()
{
    auto it = _configData.find("username");
    if(it != _configData.end())
    {
        return it->second;
    }
    LOG_ERROR("cant get username");
    return "";
}
